// Learn cc.Class:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,
    ctor() {
        // 把不在编辑器上显示的变量，放在ctor构造函数中
        this.myArray = [];
        this.goldPool = new cc.NodePool();
        this.bulletPool = new cc.NodePool();

    },
    properties: {
        logo: {
            type: cc.Node,
            default: null,
        },
        level: {
            type: cc.Node,
            default: null,
        },
        top: {
            type: cc.Node,
            default: null,
        },
        setting: {
            type: cc.Node,
            default: null,
        },
        bottom: {
            type: cc.Node,
            default: null,
        },
        getGold: {
            type: cc.Node,
            default: null,
        },
        airPlane: {
            type: cc.Node,
            default: null,
        },
        tip: {
            type: cc.Node,
            default: null,
        },
        m_GoldPrefab: {
            type: cc.Prefab,
            default: null,
        },
        m_Bg: cc.Node,
        Bullet: cc.Prefab,
        monsterHP: cc.Node,
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        gameCtrl = this;

        cc.director.getCollisionManager().enabled = true;
        // cc.director.getCollisionManager().enabledDebugDraw = true;

        this.logo.getComponent('logoAnim').play();
        let js = this.logo.getComponent('logoAnim');
        this.myArray.push(js);

        let leveljs = this.level.getComponent('levelAnim');
        this.myArray.push(leveljs);
        let topJs = this.top.getComponent('top');
        this.myArray.push(topJs);
        let settingJs = this.setting.getComponent('setting');
        this.myArray.push(settingJs);
        let bottomJs = this.bottom.getComponent('bottom');
        this.myArray.push(bottomJs);
        let getGoldJs = this.getGold.getComponent('clickGetGold');
        this.myArray.push(getGoldJs);
        let airPlaneJs = this.airPlane.getComponent('airPlane');
        this.myArray.push(airPlaneJs);
        let tipJs = this.tip.getComponent('Tip');
        this.myArray.push(tipJs);
        let BgJs = this.m_Bg.getComponent('BG');
        this.myArray.push(BgJs);
        let monterHpJs = this.monsterHP.getComponent('monsterHp');
        console.log('monterHpJs', monterHpJs);
        this.myArray.push(monterHpJs);
        for (let i = 0; i < this.myArray.length; i++) {
            console.log(this.myArray);
            if (this.myArray[i].play) {

                this.myArray[i].play();
                // console.log(this.myArray[i].play);
            }
        }

    },
    createGoldAnima(srcPos, desPos, radius, goldCount, goldAward, callback) {
        // console.log('desPos', desPos);
        // console.log('srcPos', srcPos);
        let array = this.getPoint(radius, srcPos.x, srcPos.y, goldCount);
        // console.log('array', array);
        /*准备一个数组，将离目标位置近的金币节点，排在前面，离目标近的金币，
        最先飞到目的地*/
        let nodeArray = [];

        for (let i = 0; i < array.length; i++) {
            let goldNode = this.createGold(this.node);
            goldNode.setPosition(370, 0);
            //将以起始点为中心，将将金币排列成一个圆形，然后在其位置做随机
            let randomPos = cc.v2(array[i].x + random(0, 100), array[i].y + random(0, 100));
            nodeArray.push({ goldNode, randomPos });
        }
        //对nodeArray做排序
        nodeArray.sort(function(a, b) {
            return distance(a.randomPos, desPos) - distance(b.randomPos, desPos);

        });
        // console.log('nodeArray', nodeArray);
        let topGold = this.top.getComponent('top').getGold();
        // console.log(topGold);
        let notplay = false;
        let islast = false;
        //对排好序的金币节点做动画，让所有金币以srcPos为中心散开
        for (let j = 0; j < nodeArray.length; j++) {
            islast = j == nodeArray.length - 1;
            let seq = cc.sequence(
                cc.moveTo(0.5, nodeArray[j].randomPos),
                cc.delayTime(j * 0.1),
                cc.moveTo(0.5, desPos),
                //飞过去的金币到达目的地之后，顶部的金币放大，然后缩回去
                //有很多金币，一个飞过来，动画还没有完成，又有一个金币飞过来？，没有完成动画，就让其完成动画，正在播，就不播，播放完了，才能进行下一个动作
                cc.callFunc(function(node) {
                    if (!notplay) {
                        //顶部金币缩放动画
                        let goldSeq = cc.sequence(
                            cc.scaleTo(0.05, 0.6, 0.6),
                            cc.delayTime(0.01),
                            cc.scaleTo(0.05, 0.5, 0.5),
                            cc.callFunc(function() {
                                notplay = false;
                            }));
                        topGold.runAction(goldSeq);
                    }
                    this.onKillGold(node);
                }.bind(this)));
            // 这块代码不能放在cc.callFunc中，islast中的代码执行一次，放在callFunc中，执行了多次
            if (islast) {
                // console.log('islast2',j,islast);
                gDataCtrl.AddGold(goldAward);
                if (goldAward == 0) {
                    return;
                }
                gDataCtrl.AddGold(goldAward);
                callback();
                let gold = gDataCtrl.GetGold();
                // console.log(goldCarraybit(gold));
                this.top.getComponent('top').updataView(gold);

            }
            nodeArray[j].goldNode.runAction(seq);
        }


    },
    createGold(parentNode) {
        let gold = null;
        if (this.goldPool.size() > 0) {
            gold = this.goldPool.get();
        } else {
            gold = cc.instantiate(this.m_GoldPrefab);
        }
        gold.parent = parentNode;
        return gold;
    },
    onKillGold(node) {
        this.goldPool.put(node);
    },
    createBullet(count) {
        let right = 0;
        let left = 0;
        let bullet = null;
        if (count === 1) {
            if (this.bulletPool.size() > 0) {
                bullet = this.bulletPool.get();
            } else {
                bullet = cc.instantiate(this.Bullet);
            }
            let y = this.airPlane.getPosition().y + 300;
            let x = this.airPlane.getPosition().x;
            bullet.setPosition(x, y);
            let js = bullet.getComponent('Bullet');
            js.init();
            js.setsecondPos({ x: x, y: y + 400 });
            bullet.parent = this.node;
        } else {
            for (let i = 0; i < count; i++) {
                if (this.bulletPool.size() > 0) {
                    bullet = this.bulletPool.get();
                } else {
                    bullet = cc.instantiate(this.Bullet);
                }
                let y = this.airPlane.getPosition().y + 300;
                let x = this.airPlane.getPosition().x;
                bullet.setPosition(x, y); //所有子弹的初始位置都在中心点
                let secondPos = {};

                if (i % 2 === 0) {
                    right++;
                    //secondPos是排成一列的位置，分为左边和右边，第一个子弹排在右边的第一个位置，第二个子弹排在离中心点偏移的位置
                    secondPos.x = this.airPlane.getPosition().x + right * 30 - 15;
                } else {
                    left++;
                    secondPos.x = this.airPlane.getPosition().x - left * 30 + 15;
                }
                secondPos.y = this.airPlane.getPosition().y + 400;
                let js = bullet.getComponent('Bullet');
                js.init();
                js.setsecondPos(secondPos);
                bullet.parent = this.node;
            }
        }


        return bullet;
    },
    onKillBullet(node) {
        this.bulletPool.put(node);
    },
    setAction(action) {
        if (action === ACTION_RESET) {
            this.ActionReset();
        } else if (action === ACTION_PLAY) {
            this.ActionPlay();
        } else if (action === ACTION_MOVE_IN) {
            this.ActionMoveIn();
        } else {
            this.ActionMoveOut();
        }
    },
    movePlane(pos) {
        let curPos = this.airPlane.getPosition();
        // console.log('this.airPlane',curPos,pos);
        this.airPlane.setPosition(cc.v2(curPos.x + pos.x, curPos.y + pos.y));
    },
    ActionReset() {
        for (let i = 0; i < this.myArray.length; i++) {
            if (this.myArray[i].reset) {
                this.myArray[i].reset();
            }

        }
    },
    ActionMoveIn() {
        for (let i = 0; i < this.myArray.length; i++) {
            if (this.myArray[i].moveIn) {
                this.myArray[i].moveIn();
            }

        }
    },
    ActionMoveOut() {
        for (let i = 0; i < this.myArray.length; i++) {
            if (this.myArray[i].moveOut) {
                this.myArray[i].moveOut();
            }

        }
    },
    ActionPlay() {
        for (let i = 0; i < this.myArray.length; i++) {
            if (this.myArray[i].play) {
                this.myArray[i].play();
                // console.log(this.myArray[i].play);
            }

        }
    },

    test(target, data) {

        // eslint-disable-next-line eqeqeq
        if (data == '重置') {
            gDataCtrl.AddGold(999);
            // js.reset();
        }
        if (data === 'bofang') {
            // debugger;
            for (let i = 0; i < this.myArray.length; i++) {

                if (this.myArray[i].play) {

                    this.myArray[i].play();
                    // console.log(this.myArray[i].play);

                }

            }

        }
        if (data === 'moveOut') {
            for (let i = 0; i < this.myArray.length; i++) {
                if (this.myArray[i].moveOut) {

                    this.myArray[i].moveOut();

                }
            }

        }
        if (data === 'moveIn') {
            for (let i = 0; i < this.myArray.length; i++) {
                if (this.myArray[i].moveIn) {

                    this.myArray[i].moveIn();

                }
            }
        }
    },

    getPoint: function(r, ox, oy, count) {
        let point = []; //结果
        let radians = (Math.PI / 180) * Math.round(360 / count), //弧度
            i = 0;
        for (; i < count; i++) {
            let x = ox + r * Math.sin(radians * i),
                y = oy + r * Math.cos(radians * i);

            point.unshift({ x: x, y: y }); //为保持数据顺时针
        }
        return point;
    },

    // update (dt) {},
});